import java.util.Scanner;
import java.util.Stack;
import java.util.Vector;

/**
 * This class gets the contents of the specified class.
 * @author Vincent Bantjes
 */
public class ExtractClassContents
{
    Stack<String> stack = new Stack<String>();

    /**
     * Gets the contents of the class, and if there are other contents after the closing bracket of the class, separately.
     * @param contents String. The class as a new line delimited string.
     * @return Vector<String>. A String where the first elements is the class's contents, and a second element being the contents after the class(if any). If there is no contents after the class, the second elements is "".
     */
    public Vector<String> getSeparatedClassContent(String contents)
    {
        Vector<String> splitedContent = new Vector<String>();
        String classContent ="";
        String comments = "";
        Scanner st = new Scanner(contents);
        String current ="";
        boolean fb = true;
        boolean bc = true;
        boolean quoteFlag = true;
        boolean flag = true;
        int counter = 0;
        
        while(st.hasNext())
        {
            current = st.nextLine();
            if(flag == false)
            {
                comments += current + "\n";
            }
            
            if(current.length() >= 2 && current.trim().length() >= 2 && current.trim().substring(0, 2).equals("//"))
            {}
            else
            {
                for(int i = 0; i < current.length() ;i++)
                {
                    if(current.length() >= i+2 && current.substring(i,i+2).equals("/*") && fb == true && quoteFlag == true)
                    {
                        fb = false;
                        quoteFlag = false;
                        bc = false;
                    }


                    if(current.length() >= i+2 && current.substring(i,i+2).equals("*/"))
                    {
                        fb = true;
                        quoteFlag = true;
                        bc = true;
                        i++;
                        i++;
                    }
                    
                    if(i < current.length() && current.length() >= i+2 && current.substring(i,i+2).equals("/*") && quoteFlag == true)
                    {
                        fb = false;
                    }
                    
                    if(counter == 2)
                    {
                        quoteFlag = true;
                        counter = 0;
                    }
                        
                    
                    if(i < current.length() && current.charAt(i) == '"' && fb == true)
                    {
                        if(current.charAt(i-1) != '\\')
                                counter++;
                        quoteFlag = false;
                    }

                    if(current.length() >= i+2 && current.substring(i,i+2).equals("//") && bc == true && quoteFlag == true)
                    {
                        break;
                    }
                    
                    if(i < current.length() &&fb == true && quoteFlag == true && bc == true)
                    {
                        if(current.charAt(i) == '{')
                        {
                            stack.push("{");
                        }
                        
                        if(current.charAt(i) == '}')
                        {
                            stack.pop();
                            if(stack.empty() == true)
                            {
                                flag = false;
                                if(i+1 < current.length())
                                {
                                    classContent += current.substring(0,i+1) + "\n";
                                    comments += current.substring(i+1,current.length()) + "\n";
                                }
                                else
                                {
                                    classContent += current + "\n";
                                   
                                }
                                break;
                            }
                        }
                    }                   
                }
               
                if(flag == true)
                    classContent += current + "\n";               
            }
        }
        splitedContent.add(classContent);
        splitedContent.add(comments);
        return splitedContent;
    }    
    
    /**
     * Gets the given classes contents.
     * @param contents String. A newLine delimited string representation of the class.
     * @return String. The contents of the class.
     */
    public String getClassContent(String contents)
    {
        String classContent ="";
        Scanner st = new Scanner(contents);
        String current ="";
        boolean fb = true;
        boolean bc = true;
        boolean quoteFlag = true;
        boolean addFlag = false;
        boolean flag = true;
        int counter = 0;
        while(st.hasNext())
        {
            current = st.nextLine();
            if(current.length() >= 2 && current.trim().length() >= 2 && current.trim().substring(0, 2).equals("//"))
            {
            }
            else
            {
                for(int i = 0; i < current.length() ;i++)
                {
                    if(current.length() >= i+2 && current.substring(i,i+2).equals("/*") && fb == true && quoteFlag == true)
                    {
                        fb = false;
                        quoteFlag = false;
                        bc = false;
                    }


                    if(current.length() >= i+2 && current.substring(i,i+2).equals("*/"))
                    {
                        fb = true;
                        quoteFlag = true;
                        bc = true;
                        i++;
                        i++;
                    }
                    if(i < current.length() && current.length() >= i+2 && current.substring(i,i+2).equals("/*") && quoteFlag == true)
                    {
                        fb = false;
                    }
                    
                    if(counter == 2)
                        quoteFlag = true;
                    
                    if(i < current.length() && current.charAt(i) == '"' && fb == true)
                    {
                        if(current.charAt(i-1) != '\\')
                                counter++;
                        quoteFlag = false;
                    }

                    if(current.length() >= i+2 && current.substring(i,i+2).equals("//") && bc == true && quoteFlag == true)
                    {
                        break;
                    }                  
                    if(i < current.length() &&fb == true && quoteFlag == true && bc == true)
                    {
                        if(current.charAt(i) == '{')
                        {
                            if(stack.empty() == true)
                            {
                                addFlag = true;
                            }
                            stack.push("{");
                        }
                        if(current.charAt(i) == '}')
                        {
                            stack.pop();
                            if(stack.empty() == true)
                            {
                                flag = false;
                                classContent += current.substring(0,i+1) + "\n";
                                break;
                            }
                        }
                    }                   
                }
                if(addFlag == true && flag == true)
                    classContent += current + "\n";
            }
        }
        return classContent;
    }
}
